package http;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.util.StringUtils;
import sun.net.www.http.HttpClient;

import java.io.*;
import java.net.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by User on 2017/11/22.
 */
public class Http {

    public static void main(String[] args) throws UnsupportedEncodingException {

        List list = new ArrayList();

        for (int i = 1; i < 100; i++) {
            list.addAll(getTestData(String.valueOf(i)));
        }

        System.out.println(JSON.toJSONString(list));
    }

    private static List getTestData(String page) {
        String s = sendGet("http://z.taovgo.com/itemcarrierForDQ/failTaskList?pn="+page+"&startTime=2017-03-07&endTime=2017-12-31&taskStatus=2&subTaskType=2");
        JSONObject jsonObject = JSON.parseObject(s);
        JSONArray res = jsonObject.getJSONArray("res");
        List list = new ArrayList();
        if (res == null) return list;
        for (Object object : res) {
            JSONObject item = (JSONObject) object;
            String cid = item.getString("cid");
            String numiid = item.getString("numIid");
            if (cid.equals("0")) continue;
            Map map = new HashMap();
            map.put("cid", cid);
            map.put("numiid", numiid);
            list.add(map);
        }

        return list;
    }

    private static List getItemIdsByKeyWords(String key) {
        String gbk = URLEncoder.encode(key);
        String s = sendGet("http://data.p4psearch.1688.com/data/ajax/search_get_ldp_supplyoffer_html.json?beginpage=1&asyncreq=1&keywords=" + gbk + "&sortType=&descendOrder=&province=&city=&priceStart=&priceEnd=&trackid=885688110390753370624704&cosite=baidujj&location=landing_t4&fcatid=&p4pid=1512376884592066135879&se=&bt=&pageid=b79d42870be0c23a1512376884&_=1512376921777");
        JSONObject jsonObject = JSON.parseObject(s);

        String sss = jsonObject.getJSONObject("data").getJSONObject("content").getString("p4pAttrs");
        List<String> result = new ArrayList<>();
        for (String a : sss.split(";")) {
            a = a.substring(0, a.indexOf(","));
            result.add(a);
        }
        return result;
    }

    interface HttpSender {
        String sendPost(String url, Map<String, Object> params, String charset);

        default String concatParams(Map<String, Object> params) {
            // 构建请求参数
            StringBuilder sbParams = new StringBuilder();
            if (params != null && params.size() > 0) {
                for (Map.Entry<String, Object> e : params.entrySet()) {
                    sbParams.append(e.getKey());
                    sbParams.append("=");
                    sbParams.append(e.getValue());
                    sbParams.append("&");
                }
            }
            return sbParams.toString();
        }
    }

    // 使用HttpURLConnection发送post请求
    public class HttpURLConnectionSender implements HttpSender {
        @Override
        public String sendPost(String url, Map<String, Object> params, String charset) {
            StringBuffer resultBuffer = null;
            HttpURLConnection con = null;
            try {
                con = (HttpURLConnection) (new URL(url)).openConnection();
                con.setRequestMethod("POST");
                con.setDoOutput(true);
                con.setDoInput(true);
                con.setUseCaches(false);
                con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                String paramsData = concatParams(params);
                if (paramsData != null && paramsData.length() > 0) {
                    try (OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream(), "UTF-8")) {
                        osw.write(paramsData.substring(0, paramsData.length() - 1));
                        osw.flush();
                    }
                }
                // 读取返回内容
                StringBuilder sb = new StringBuilder();
                InputStream inputStream = con.getInputStream();
                byte[] bs = new byte[1024];
                int len  = -1;
                while ((len = inputStream.read(bs)) > 0) {
                    String s = new String(bs, 0, len, "UTF-8");
                    sb.append(s);
                }

                return sb.toString();

                /*resultBuffer = new StringBuffer();
                try (BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "GBK"))) {
                    String temp;
                    while ((temp = br.readLine()) != null) resultBuffer.append(temp);
                }*/

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (con != null) {
                    con.disconnect();
                    con = null;
                }
            }

            return resultBuffer.toString();
        }
    }

    // 使用HttpClient发送post请求
    public class HttpClientSender implements HttpSender {
        @Override
        public String sendPost(String url, Map<String, Object> params, String charset) {

            CloseableHttpClient client = HttpClientBuilder.create().build();

            HttpPost post = new HttpPost(url);

            List<NameValuePair> paramsList = new ArrayList<>();
            Iterator<Map.Entry<String, Object>> iterator = params.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, Object> next = iterator.next();
                paramsList.add(new BasicNameValuePair(next.getKey(), String.valueOf(next.getValue())));
            }

            if (!paramsList.isEmpty()) {
                UrlEncodedFormEntity entity = null;
                try {
                    entity = new UrlEncodedFormEntity(paramsList, charset);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                post.setEntity(entity);
            }

            StringBuilder stringBuilder = new StringBuilder();
            try (CloseableHttpResponse execute = client.execute(post)) {
                HttpEntity entity = execute.getEntity();
                InputStream content = entity.getContent();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(content));
                String line;
                while ((line = bufferedReader.readLine()) != null) {
                    stringBuilder.append(line).append("\r\n");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            return stringBuilder.toString();
        }
    }


    public static String sendGet(String url) {
        String result = "";
        BufferedReader in = null;
        try {
            String urlNameString = url;
            URL realUrl = new URL(urlNameString);

            // 打开和URL之间的连接
            URLConnection connection = realUrl.openConnection();
            // 设置通用的请求属性
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

            // 建立实际的连接
            connection.connect();
            // 获取所有响应头字段
            Map<String, List<String>> map = connection.getHeaderFields();
            // 遍历所有的响应头字段
            for (String key : map.keySet()) {
                //System.out.println(key + "--->" + map.get(key));
            }
            // 定义 BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(
                    connection.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送GET请求出现异常！" + e);
            e.printStackTrace();
        }
        // 使用finally块来关闭输入流
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result;
    }

    /**
     * 向指定 URL 发送POST方法的请求
     *
     * @param url 发送请求的 URL
     * @param param 请求参数，请求参数应该是 name1=value1&name2=value2 的形式。
     * @return 所代表远程资源的响应结果
     */
    public static String sendPost(String url, String param, String encode) throws IOException {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        if(StringUtils.isEmpty(encode)) {
            encode = "GBK";
        }
        try {
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            URLConnection conn = realUrl.openConnection();
            // 设置通用的请求属性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            out = new PrintWriter(conn.getOutputStream());
            // 发送请求参数
            out.print(param);
            // flush输出流的缓冲
            out.flush();
            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(conn.getInputStream(), encode));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
                result += "\n";
            }
            return result;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            out.close();
            in.close();
        }
        return result;
    }

    public static String directGet(String url, String referer, HttpHost host, String cookies) {
        org.apache.http.client.HttpClient httpclient = null;

        HttpResponse rsp = null;

        try {
            httpclient = new DefaultHttpClient();
            if (host != null) {
                httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, host);
            }

            HttpConnectionParams.setSoTimeout(httpclient.getParams(), 30000);
            HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), 30000);
            httpclient.getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
            httpclient.getParams().setParameter(ClientPNames.MAX_REDIRECTS, 20);

            HttpGet httpGet = new HttpGet(url);
            if (referer != null) {
                httpGet.addHeader("Referer", referer);
            }
            if (cookies != null) {
                httpGet.addHeader("Cookie", cookies);
            }

//            httpGet.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)");
            httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36");

            rsp = httpclient.execute(httpGet);

            HttpEntity entity = rsp.getEntity();
            String content = EntityUtils.toString(entity);
            EntityUtils.consume(entity);

            return content;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
